const mongoose = require("mongoose");
async function createFirst(dateStr, callback) {
	var arr = await mongoose.model("chartsModel").find({ date: dateStr });
	if (arr.length == 0) {
		mongoose.model("chartsModel").create({
			date: dateStr,
			admins: 0,
			users: 0,
			orders: 0
		});
	}
	return await callback();
}
module.exports.updateStatistics3 = async key => {
	var today = new Date().toLocaleDateString();
	return await createFirst(today, async function() {
		mongoose
			.model("totalChartsModel")
			.updateOne({ type: "all" }, { $inc: { [key]: 1 } });
		return await mongoose
			.model("chartsModel")
			.updateOne({ date: today }, { $inc: { [key]: 1 } });
	});
};
module.exports.getListStatistics3 = async () => {
	var today = new Date().toLocaleDateString();
	return await createFirst(today, async function() {
		return await mongoose.model("chartsModel").find();
	});
};
module.exports.getTodayStatistics3 = async () => {
	var today = new Date().toLocaleDateString().replace(/-/g, "/");
	return await createFirst(today, async function() {
		return await mongoose.model("chartsModel").find({ date: today });
	});
};
module.exports.getTotalStatistics3 = async () => {
	return await mongoose.model("totalChartsModel").find({ type: "all" });
};
